package com.xuequ.answerme.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xuequ.answerme.R;
import com.xuequ.answerme.beans.AnswerBean;
import com.xuequ.answerme.beans.QuestionBean;
import com.xuequ.answerme.beans.UserBean;
import com.xuequ.answerme.manager.AnswerManager;
import com.xuequ.answerme.manager.QuestionManager;
import com.xuequ.answerme.manager.UserManager;
import com.xuequ.answerme.settings.SettingsActivity;
import com.xuequ.answerme.tools.Tools;

import java.util.List;

import cn.bmob.v3.BmobUser;

/**
 * Created by Shijie on 2015/9/28.
 */
public class MeFragment extends Fragment {

    private TextView mUsername;
    private TextView mSignature;
    private TextView mQuestions;
    private TextView mAnswers;
    private TextView mFocus;
    private TextView mLike;
    private ImageView mSettings;

    private UserManager userManager;
    private AnswerManager answerManager;
    private QuestionManager questionManager;

    private UserBean userBean;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_me, container, false);

        initViews(view);

        return view;

    }

    private void initViews(View view) {

        if (userManager == null) {
            userManager = new UserManager();
        }
        if (answerManager == null) {
            answerManager = new AnswerManager();
        }
        if (questionManager == null) {
            questionManager = new QuestionManager();
        }

        userBean = BmobUser.getCurrentUser(getActivity(), UserBean.class);

        mSettings = (ImageView) view.findViewById(R.id.me_settings_img);
        mSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SettingsActivity.class));
//                startActivityForResult(null, Tools.ACTIVITY_SETTINGS);
            }
        });
        mSignature = (TextView) view.findViewById(R.id.me_signature);
        mUsername = (TextView) view.findViewById(R.id.me_username);
        mQuestions = (TextView) view.findViewById(R.id.me_question_text);
        mAnswers = (TextView) view.findViewById(R.id.me_answer_text);
        mFocus = (TextView) view.findViewById(R.id.me_focus_text);
        mLike = (TextView) view.findViewById(R.id.me_like_text);

        setText();

    }

    private void setText() {

        mUsername.setText(userBean.getUsername());


        if (!TextUtils.isEmpty(userBean.getSignature())) {
            mSignature.setText(userBean.getSignature());
        }

        userManager.allMyQuestionsCount(getActivity(), userBean, new UserManager.FindAllMyQuestionsListener() {
            @Override
            public void findAllMyQuestionsOnSuccess(List<QuestionBean> questions, int count) {
                mQuestions.setText("?:" + count);
            }

            @Override
            public void findAllMyQuestionsOnFailure(String failureMsg) {

            }
        });

        answerManager.allMyAnswersCount(getActivity(), userBean, new AnswerManager.FindAllMyAnswersListener() {
            @Override
            public void findMyAnswersOnSuccess(List<AnswerBean> answers, int count) {
                mAnswers.setText("!:" + count);
            }

            @Override
            public void findMyAnswersOnFailure(String failureMsg) {

            }
        });


        userManager.allFocusCount(getActivity(), userBean, new UserManager.FindFocusListener() {
            @Override
            public void findFocusOnSuccess(List<QuestionBean> questionBeans, int count) {
                mFocus.setText("★:" + count);
            }

            @Override
            public void findFocusOnFailure(String failureMsg) {

            }
        });


        userManager.allLikeCount(getActivity(), userBean, new UserManager.FindLikeListener() {
            @Override
            public void findLikeOnSuccess(List<AnswerBean> answerBeans, int count) {
                mLike.setText("★:" + count);
            }

            @Override
            public void findLikeOnFailure(String failureMsg) {

            }
        });

    }

}


